from rest_framework import serializers

from goods.models import SKU


class CartSerializer(serializers.Serializer):
    # 增加字段
    sku_id = serializers.IntegerField(label="sku_id", min_value=1)
    count = serializers.IntegerField(label="商品sku id", min_value=1)
    selected = serializers.BooleanField(label="是否勾选，默认勾选", default=True)

    # 验证数据
    def validate_sku_id(self, value):
        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError("sku_id 不存在")

        return value


class CartSKUSerializer(serializers.ModelSerializer):
    """
    序列化输出商品模型
    """
    count = serializers.IntegerField(label="商品sku id")
    selected = serializers.BooleanField(label="是否勾选，默认勾选")
    class Meta:
        model = SKU
        fields = ["id", "count", "selected", "name", "default_image_url", "price"]


class CartDeleteSeriazlier(serializers.Serializer):
    """
    删除的序列化器
    """
    sku_id = serializers.IntegerField(label="sku_id", min_value=1)

    def validate_sku_id(self, value):
        try:
            SKU.objects.get(id=value)
        except SKU.DoesNotExist:
            raise serializers.ValidationError("sku_id不存在")
        return value


class CartSelectedAllSerializer(serializers.Serializer):
    selected = serializers.BooleanField(label="是否勾选，默认勾选")
